package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.OfferMapper;
import com.springboot_gradrecruit.model.dto.offer.AddOfferRequest;
import com.springboot_gradrecruit.model.dto.offer.PageOfferRequest;
import com.springboot_gradrecruit.model.dto.offer.UpdateOfferRequest;
import com.springboot_gradrecruit.model.entity.Offer;
import com.springboot_gradrecruit.model.vo.offer.OfferVO;
import com.springboot_gradrecruit.service.OfferService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author zr
* @description 针对表【offer】的数据库操作Service实现
* @createDate 2024-12-26 09:34:51
*/
@Service
public class OfferServiceImpl extends ServiceImpl<OfferMapper, Offer>
    implements OfferService {

    @Resource
    private OfferMapper offerMapper;

    @Override
    public Long addOffer(AddOfferRequest addOfferRequest) {

        //1.获取参数
        Offer offer = new Offer();
        offer.setOfferStudentId(addOfferRequest.getOfferStudentId());
        offer.setOfferJobId(addOfferRequest.getOfferJobId());
        offer.setOfferEnterpriseId(addOfferRequest.getOfferEnterpriseId());
        offer.setOfferExpirationDate(addOfferRequest.getOfferExpirationDate());
        offer.setOfferStatus(addOfferRequest.getOfferStatus());
        offer.setOfferSupplementaryInformation(addOfferRequest.getOfferSupplementaryInformation());

        //2.插入数据
        int insert = offerMapper.insert(offer);

        //3.返回结果
        return (long)insert;
    }

    @Override
    public PageResponse<OfferVO> pageOffer(PageOfferRequest pageOfferRequest) {
        //1.获取参数
        String offerStatus = pageOfferRequest.getOfferStatus();
        Long offerStudentId = pageOfferRequest.getOfferStudentId();
        Long offerEnterpriseId = pageOfferRequest.getOfferEnterpriseId();

        //2.构建查询条件
        Page<Offer> page = pageOfferRequest.toPageDefaultSortByUpdateTime();

        if(offerStudentId!=null&&offerEnterpriseId!=null){
            throw new RuntimeException("offerStudentId和offerEnterpriseId不能同时存在");
        }

        //3.执行查询
        Page<Offer> offerPage;
        if(offerStudentId!=null){
            offerPage = lambdaQuery()
                    .eq(StrUtil.isNotBlank(offerStatus), Offer::getOfferStatus, offerStatus)
                    .eq(true, Offer::getStudentIsDeleted, "未删除")
                    .page(page);
        }else {
            offerPage = lambdaQuery()
                    .eq(StrUtil.isNotBlank(offerStatus), Offer::getOfferStatus, offerStatus)
                    .eq(true, Offer::getEnterpriseIsDeleted, "未删除")
                    .page(page);
        }
        return PageResponse.of(offerPage, OfferVO.class);

    }

    @Override
    public boolean updateOfferById(UpdateOfferRequest updateOfferRequest) {
        //1.获取参数
        String offerId = updateOfferRequest.getOfferId();
        String offerStatus = updateOfferRequest.getOfferStatus();

        //2.更新Offer
        LambdaUpdateWrapper<Offer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Offer::getOfferId, offerId)
                .set(StrUtil.isNotBlank(offerStatus), Offer::getOfferStatus, offerStatus);
        return offerMapper.update(null, updateWrapper) > 0;

    }

}




